package cn.yunhe.service;

import cn.yunhe.dao.ProductDao;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.Product;
import cn.yunhe.util.PageUtil;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class ProductService {
    SqlSession session=SqlSessionFactoryUtil.getSession();
    ProductDao productDao=session.getMapper(ProductDao.class);
    //分页查看所有商品
    public List<Product> getAllproBypage(int currage){
        Page page=new Page();
        page.setPageStart(PageUtil.PAGE_SIZE*(currage-1));
        page.setPageSize(PageUtil.PAGE_SIZE);
        List<Product> products=productDao.getAllproBypage(page);
        return products;
    }
    public List<Product> getAllproBypage2(Page p){
        Page page = new Page();
        page.setPageStart(PageUtil.PAGE_SIZE*(p.getPageStart()-1));
        page.setPageSize(PageUtil.PAGE_SIZE);
        page.setKeyword(p.getKeyword());
        page.setEpcname(p.getEpcname());
        List<Product> products=productDao.getAllproBypage(page);
        return products;
    }
    //商品总数量
    public int getProCount(){
      return   productDao.getProCount();
    }
    //根据ID查找商品详情
        public Product getById(int epid){

        return productDao.getById(epid);
    }
    //根据epid删除商品
    public boolean delProByEpid(int epid){
        boolean flag=productDao.delProByEpid(epid);
        session.commit();
        session.close();
        return flag;
    }
    //根据epid修改商品
    public boolean getProByEpid(Product product){
        boolean b =  productDao.getProByEpid(product);
        session.commit();
        session.close();
        return  b;
    }
    //根据epid增加商品
    public  boolean addProByEpid(int epid){
        boolean flag=productDao.addProByEpid(epid);
        session.commit();
        return  flag;
    }
    //模糊查询商品
    public List<Product> getAllByName(int currage,String keyword){
        Page page=new Page();
        page.setPageStart(PageUtil.PAGE_SIZE*(currage-1));
        page.setPageSize(PageUtil.PAGE_SIZE);
        page.setKeyword(keyword);
        List<Product> productList=productDao.getAllByName(page);
        return productList;
    }

    //根据父类id查询对应旗下的所有商品
     public  List<Product> getAllByParentid(int epcpartentid){
        return productDao.getAllByParentid(epcpartentid);
}
//查询特卖商品
    public  List<Product> getMinPrice(){
        return productDao.getMinPrice();
    }
    public  Boolean createProduct(Product product){
        Boolean b = productDao.createProduct(product);
        session.commit();
        session.close();
        return  b;
    }

    public List<Product> getSDtock(){
        List<Product> productList = productDao.getSDtock();
        return  productList;
    }

    /**
     * 郭瀚钰新闻模块用
     *     根据子分类id分页获取商品信息
     * @param pageIndex
     * @param pageSize
     * @param epcid
     * @return
     */
    public List<Product> getProductByChildClassId(int pageIndex,int pageSize,int epcid){
        session = SqlSessionFactoryUtil.getSession();
        productDao=session.getMapper(ProductDao.class);
        int pageStart = (pageIndex-1)*pageSize;
        Page page =  new Page(pageStart,pageSize,epcid);
        List<Product> list = new ArrayList<Product>();
        list =  productDao.getProductByChildClassId(page);
        session.close();
        return list;
    }

    /**
     * 郭瀚钰新闻模块用
     *          根据子分类id分页获取商品信息总记录数
     * @param epcid
     * @return
     */
    public int getProductByChildClassIdCount(int epcid){
        session = SqlSessionFactoryUtil.getSession();
        productDao=session.getMapper(ProductDao.class);
        Page page =  new Page(epcid);
        Integer num =  productDao.getProductByChildClassIdCount(page);
        session.close();

        return num;
    }
 }
